package com.dudy.authdemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.dudy.authdemo.model.Menu;
import com.dudy.authdemo.model.RolePermission;
import com.dudy.authdemo.service.RolePermissionService;
import com.dudy.authdemo.vo.PersonVO;
import com.dudy.authdemo.vo.RolePermissionVO;
import com.dudy.common.base.CommonResponse;
import com.dudy.common.base.ResultCode;
import com.dudy.common.util.DingTalkUtil;
import com.dudy.common.util.ResponseUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: auth_demo
 * @author: dudy
 * @create: 2020-12-24 10:43
 * @description:
 **/
@RestController
@RequestMapping("/rolePermission")
public class RolePermissionController {

    private static final Logger log = LoggerFactory.getLogger(RolePermissionController.class);

    @Autowired
    private RolePermissionService rolePermissionService;

    @PostMapping("/grantPermission")
    public CommonResponse<RolePermission> grantPermission(RolePermissionVO rolePermissionVO) {
        if (null == rolePermissionVO || StringUtils.isBlank(rolePermissionVO.getRoleCode()) || CollectionUtils.isEmpty(rolePermissionVO.getPermissionIds())) {
            return ResponseUtils.getResponse(ResultCode.REQUEST_PARAM_NULL);
        }
        List<Long> permissionIds = rolePermissionVO.getPermissionIds();
        ArrayList<RolePermissionVO> rolePermissionVOS = new ArrayList<>(permissionIds.size());
        permissionIds.forEach(permissionId -> {
            RolePermissionVO rolePermissionVO1 = new RolePermissionVO();
            rolePermissionVO1.setPermissionType((byte) 0);
            rolePermissionVO1.setRoleCode(rolePermissionVO.getRoleCode());
            rolePermissionVO1.setPermissionValue(permissionId);
            rolePermissionVOS.add(rolePermissionVO1);
        });
        try {
            rolePermissionService.insertBatch(rolePermissionVOS);
            return ResponseUtils.getResponse(ResultCode.SUCCESS);
        } catch (Exception e) {
            log.error("RolePermissionController grantPermission error,请求参数：{}", JSONObject.toJSONString(rolePermissionVO), e);
            DingTalkUtil.pushError("auth_demo", this.getClass().getName(), "grantPermission", JSONObject.toJSONString(rolePermissionVO)
                    , e);
            return ResponseUtils.getResponse(ResultCode.FAIL);
        }
    }

    @GetMapping("/queryPermissionByRoleCode")
    public CommonResponse<List<Long>> queryPermissionByRoleCode(@RequestParam("roleCode") String roleCode) {
        Example example = new Example(RolePermission.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleCode", roleCode);
        try {
            List<RolePermissionVO> rolePermissionVOS = rolePermissionService.selectByExample(example);
            if (CollectionUtils.isEmpty(rolePermissionVOS)) {
                return ResponseUtils.getResponse(ResultCode.SUCCESS);
            } else {
                List<Long> permissionIds = new ArrayList<>(rolePermissionVOS.size());
                rolePermissionVOS.forEach(rolePermissionVO -> {
                    permissionIds.add(rolePermissionVO.getPermissionValue());
                });
                return ResponseUtils.getResponse(ResultCode.SUCCESS, permissionIds);
            }
        } catch (Exception e) {
            log.error("RolePermissionController queryPermissionByRoleCode error,请求参数：{}", roleCode, e);
            DingTalkUtil.pushError("auth_demo", this.getClass().getName(), "queryPermissionByRoleCode",
                    roleCode + "",
                    e);
            return ResponseUtils.getResponse(ResultCode.FAIL);
        }
    }
}
